from src.util.Route import *
from src.service import GroupManageService


@route('/group/get', methods=['POST'])
def getAllGroup():
    data = request.json
    num = data.get("num")
    size = data.get("size")
    cdn = data.get('cdn')
    page = Page(size=size, num=num)
    return R.success(GroupManageService.getGroup(page, cdn))


@route('/group/add', methods=['POST'])
def addGroup():
    data = request.json
    data['createBy'] = getUid()
    r = GroupManageService.addGroup(data)
    if r:
        return R.success(r, '添加成功')
    return R.bad_request('添加失败')


@route('/group/del', methods=['POST'])
def delGroup():
    data = request.json
    r = GroupManageService.delGroup(data)
    if r:
        return R.success('', '删除成功')
    return R.bad_request('删除失败')


@route('/group/update', methods=['POST'])
def updateGroup():
    data = request.json
    data['updateBy'] = getUid()
    r = GroupManageService.updateGroup(data)
    if r:
        return R.success('', '更新成功')
    return R.bad_request('更新失败')


@route('/group/user/get')
def getGroupUser():
    gid = int(request.args.get('id'))
    result = GroupManageService.getGroupUser(gid)
    return R.success(result)


@route('/group/user/update', methods=['POST'])
def updateGroupUser():
    data = request.json
    gid = int(request.args.get('id'))
    r = GroupManageService.updateGroupUser(gid, data, getUid())
    if r:
        return R.success('', '更新成功')
    return R.bad_request('更新失败')


@route('/group/auth/get')
def getGroupAuth():
    gid = int(request.args.get('id'))
    gtype = request.args.get('type')
    result = GroupManageService.getGroupAuth(gid, gtype)
    return R.success(result)


@route('/group/auth/update', methods=['POST'])
def updateGroupAuth():
    data = request.json
    gid = int(request.args.get('id'))
    gtype = request.args.get('type')
    r = GroupManageService.updateGroupAuth(gid, gtype, data, getUid())
    if r:
        return R.success('', '更新成功')
    return R.bad_request('更新失败')
